window._MTZCJ = {

    _MAINLABEL:null,

    _CHAPTERNMB:[5,10,15,20,25,30,35,41],

    closeUi:function(){
        if(_MTZCJ._MAINLABEL){
            _GRM.putNode(_MTZCJ._MAINLABEL);
            _MTZCJ._MAINLABEL = null;
        }
    },

    showUi:function(parentNode,param){

        var uiObj = _GRM.getNodeObj('cenjiuTz');
        var mainLabel = uiObj.node;
        _MTZCJ._MAINLABEL = mainLabel;
        if(uiObj.isnew){
            _MTZCJ.initButton(mainLabel);
        }

        parentNode.addChild(mainLabel);

        _MTZCJ.refreshAll();

        return mainLabel;

    },

    initButton:function(mainLabel){

        var maskNode = mainLabel.getChildByName('mask');
        maskNode.on(cc.Node.EventType.TOUCH_START, function(){
            _MTZCJ.closeUi();
        }, maskNode);

        var tzNode = mainLabel.getChildByName('leftLabel')
        .getChildByName('troops')
        .getChildByName('troops')
        .getChildren();
        for(var i=0;i<tzNode.length;i++){
            var btn = tzNode[i].getChildByName('btnReward');
            btn.tzcjIndex = i;
            btn.on(cc.Node.EventType.TOUCH_START, function(){
                console.log(this.tzcjIndex);
                if(_MTZCJ.isGet(this.tzcjIndex)){
                    _TIPS.showMessage('该奖励已经领取');
                }else{
                    _MTZCJ.setGet(this.tzcjIndex);
                    _TIPS.showMessage('获得重生丹*1');
                    _GAMEDATAHELPER.pushItem(_IOF.createObjByName('重生丹'));
                    _MTZCJ.refreshAll();
                }
            }, btn);
        }

    },

    refreshAll:function(){
        var mainLabel = _MTZCJ._MAINLABEL;
        var tzNode = mainLabel.getChildByName('leftLabel')
        .getChildByName('troops')
        .getChildByName('troops')
        .getChildren();
        var currentnmb = _MTZCJ.getCurrentNmb();
        //var totalnmb = _MTZCJ.getTotalNmb();
        for(var i=0;i<tzNode.length;i++){
            var node = tzNode[i];
            var nmb = _MTZCJ._CHAPTERNMB[i];
            var titleNode = node.getChildByName('title');
            var expNode = node.getChildByName('exp');
            var btnReward = node.getChildByName('btnReward');

            titleNode.getComponent(cc.Label).string = "通关关卡"+currentnmb+"/"+nmb;
            expNode.getChildByName("progress").getComponent(cc.ProgressBar).progress = currentnmb/nmb;
            expNode.getChildByName('title').getComponent(cc.Label).string = currentnmb+"/"+nmb;
            if(currentnmb>=nmb){
                btnReward.active = true;
                if(_MTZCJ.isGet(i)){
                    btnReward.getChildByName('Background')
                    .getChildByName('Label').getComponent(cc.Label).string = "已领取";
                }else{
                    btnReward.getChildByName('Background')
                    .getChildByName('Label').getComponent(cc.Label).string = "领取奖励";
                }
            }else{
                btnReward.active = false;
            }
        }
    },

    isGet:function(index){

        if(!window.gameData.tzcjgetlog){
            window.gameData.tzcjgetlog = {};
        }
        return window.gameData.tzcjgetlog[index];
    },

    setGet:function(index){
        if(!window.gameData.tzcjgetlog){
            window.gameData.tzcjgetlog = {};
        }

        window.gameData.tzcjgetlog[index]=1;
    },

    getTotalNmb:function(){
        return 41;
    },

    getCurrentNmb:function(){
        var currentnmb = 0;
        for(var i=0;i<_CTZ.length;i++){
            if(_GAMEDATAHELPER.getTzLog(i)){
                currentnmb++;
            }
        }
        return currentnmb;
    }

}